﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlServerCe;

namespace AlumnesBD
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }


        private void dataGridView1_CellContentClick_1(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void dataGridView1_CellContentClick_2(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void dataGridView1_CellContentClick_3(object sender, DataGridViewCellEventArgs e)
        {
            
        }

        private void Form1_Load_1(object sender, EventArgs e)
        {
            SqlCeConnection con = new
                 SqlCeConnection("Data Source=|DataDirectory|\\Alumnes.sdf");

            con.Open();
            SqlCeCommand com = new SqlCeCommand("SELECT * FROM Alumne", con);
            SqlCeDataReader r = com.ExecuteReader();
            //Afegir columnes dins el control
            dataGridView1.Columns.Add(new DataGridViewCheckBoxColumn());
            while (r.Read())
            {
                //Creo una fila
                Object[] arr = new Object[2];
                arr[0] = r["Nom"];
                arr[1] = r["Cognom"];

                //Afegeixo la fila
                dataGridView1.Rows.Add(arr);
            }
        }
       
        private void button1_Click(object sender, EventArgs e)
        {
            ModificarUsuari m = new ModificarUsuari();

            //Accedir al datagrid i agafar l'alumne seleccionat ara mateix.
            //m.setIdAlumne(id);
            m.Show();
            this.Hide();
        }

        private void menu_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            mostraUsuari();
        }

        private void mostraUsuari()
        {
            //Seleccionar ID de l'alumne.
            DataGridViewSelectedRowCollection fSeleccionades = 
                dataGridView1.SelectedRows;

            if (fSeleccionades.Count > 0)
            {
                int id = (int)fSeleccionades[0].Cells[0].Value;

                //Carregar un alumne de la BD
                AlumneConversor abd = new AlumneConversor();
                abd.ID = id;
                abd.load();

                FrmVeureDadesAlumnes frmDa = new FrmVeureDadesAlumnes();
                frmDa.setAlumne(abd);
                frmDa.Show();
            }
            /*
            DataGridViewSelectedRowCollection filesSeleccionades = dataGridView1.SelectedRows;
            if (filesSeleccionades.Count > 0)
            {  
                Alumne a = new Alumne();
                a.Nom1 = (string)filesSeleccionades[0].Cells[0].Value;
                a.Cognom1 = (string)filesSeleccionades[0].Cells[1].Value;

                /*a.setNom("Xevi");
                //a.setCognom("Terradellas");
                a.Cognom1 = "Terradellas";
                Console.WriteLine(a.Cognom1);
                //Mostra el formulari
                FrmVeureDadesAlumnes frm = new FrmVeureDadesAlumnes();
                frm.setAlumne(a);
                frm.Show();
            
            }*/
        }

        private void button2_Click(object sender, EventArgs e)
        {
            NouUsuari f = new NouUsuari();
            f.Show();
            this.Hide();
        }
    }
}
//Clase per no repetir codi, no tenir que tornar a conectar
